﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;


using HA.ModFlow.Infrastructure;

using HAUtilWpf;

using System.Data;





namespace HA.ModFlow.Controls
{
   public class PCCellCenterDS:PCBase
    {

      public PCCellCenterDS(CellCenterDataSet3d dataset)
       {

           this._dataset = dataset;
           this.InitData();
           

       }


       protected void InitData()
       {

           this._layerIndex.Clear();

           this._lstData.Clear();

           for (int k = 0; k < this._dataset.Nlayers; k++)
           {
               this._layerIndex.Add(k + 1);


               double[,] array=new double[this._dataset.Nrows,this._dataset.Ncols];

               for (int i = 0; i < this._dataset.Nrows; i++)
               {
                   for (int j = 0; j < this._dataset.Ncols; j++)
                   {

                       array[i, j] = this._dataset.Datas[i, j, k];
                       
                   }
                   
               }



               this._lstData.Add(array);

           }

           this._selectedIndex = 0;







       }

       List<double[,]> _lstData = new List<double[,]>();

       public List<double[,]> LstData
       {
           get { return _lstData; }
           set { _lstData = value; }
       }


       /// <summary>
       /// 用于表示可选的数据
       /// </summary>
       List<int> _layerIndex = new List<int>();

       public List<int> LayerIndex
       {
           get { return _layerIndex; }
           set { _layerIndex = value; }
       }


       CellCenterDataSet3d _dataset;

       public CellCenterDataSet3d Dataset
       {
           get { return _dataset; }

       }



       int _selectedIndex;


       /// <summary>
       /// 当前选择编辑的图层
       /// </summary>
       public int SelectedIndex
       {
           get { return _selectedIndex; }
           set {


               if (this._selectedIndex!=value)
               {

                   _selectedIndex = value;

                   this.OnSetSelectedIndex();

                   
               }
               
              

           
           }
       }


     

       public DataView CurrentData
       {
           get { 
               
               return

                   PCGridArrayLayer<double>.GetBindable2DArray<double>(this._lstData[this._selectedIndex]);
                   
                  
                   
                 
           
           
           }
        
       }



       protected void OnSetSelectedIndex()
       {

           this.OnPropertyChanged("CurrentData");
           
 
       }


       public double[,] CurrentLayerDouble
       {

           get
           {
               return this._lstData[this._selectedIndex];
           }
       }


       ////重置层为单一值
       //public void ResetIntData(int data)
       //{
       //    //int index = this.SelectedIndex;

       //    for (int i = 0; i < this._dataset.Nrows; i++)
       //        for (int j = 0; j < this._dataset.Ncols; j++)
       //        {
       //            this._dataset.Datas[i,j,this._selectedIndex] = data;
       //        }


         
       //}



       public void ResetDoubleData(double data)
       {


           foreach (int index in this._layerIndex)
           {
               for (int i = 0; i < this._dataset.Nrows; i++)
                   for (int j = 0; j < this._dataset.Ncols; j++)
                   {

                       this._lstData[index - 1][i, j] = data;
                   }
           }

         
       }


       public void ResetDoubleDataCurrentLayer(double data)

       {

           for (int i = 0; i < this._dataset.Nrows; i++)
               for (int j = 0; j < this._dataset.Ncols; j++)
                   this._lstData[this._selectedIndex][i, j] = data;

 

       }



       public void Reset3DArryDataToListData(double [,,] newValue)
       {

           if (newValue == null)
           {
               return;

           }
           else
           {

               //this._lstData.Clear();//哈哈  这个 WPF 好怪  我把值 Clear 掉 再重新给 它就不主动刷新显示了 非得这样挨个给值  要是新数据的Row，Col，Layer变了怎么办呀？


               for (int layer = 0; layer < newValue .GetLength (2); layer++)
               {
                   //double[,] temp = new double[newValue.GetLength(0), newValue.GetLength(1)];

                   for (int row = 0; row < newValue .GetLength (0); row++)
                   {
                       for (int col = 0; col < newValue .GetLength (1); col++)
                       {

                          //temp[row, col] = newValue[row, col, layer];


                           this._lstData[layer][row, col] = newValue[row, col, layer];     
                

                       }
                       
                   }

                   //_lstData.Add(temp );

               }


           }
       


       
       
       }



       ////重置选中区域为单一值
       //public void ResetPartIntData(int data)
       //{
       //    //int index = this.SelectedIndex;

       //    //IList<DataGridCellInfo> selectedCells = this.dgData.SelectedCells;
       //    //int count = this.dgData.SelectedCells.Count;



       //    //for (int i = 0; i < count; i++)
       //    //{
       //    //    //相办法获取正确的行列
       //    //    //selectedCells[i].in



       //    //    //var ob= (DataGridRow)this.dgData.ItemContainerGenerator.ContainerFromItem(selectedCells[i].Item);

       //    //    DataRowView drv = (DataRowView)selectedCells[i].Item;
       //    //    DataTable dt = drv.Row.Table;
       //    //    int rowIndex = dt.Rows.IndexOf(drv.Row);



       //    //    //int row = DataGridHelper.GetRowIndex(cell);
       //    //    int column = selectedCells[i].Column.DisplayIndex;



       //    //    this._intData[index][rowIndex, column] = data;
       //    //    DataGridColumn dgcolumn = this.dgData.ColumnFromDisplayIndex(column);



       //    //}
       //    //this.dgData.Items.Refresh();

       //}

    


       ////重置所有层为单一值
       //public void AllResetIntData(int data)
       //{
       //    //foreach (int index in this._layerIndex)
       //    //{
       //    //    for (int i = 0; i < this._intData[index - 1].GetLength(0); i++)
       //    //        for (int j = 0; j < this._intData[index - 1].GetLength(1); j++)
       //    //        {
       //    //            this._intData[index - 1][i, j] = data;
       //    //        }
       //    //}
       //    //this.dgData.Items.Refresh();
       //}

       //public void AllResetDoubleData(double data)
       //{
       //    foreach (int index in this._layerIndex)
       //    {
       //        for (int i = 0; i < this._dataset.Nrows; i++)
       //            for (int j = 0; j < this._dataset.Ncols; j++)
       //            {
       //                this._dataset.Datas[i, j, index-1] = data;
       //                //this._doubleData[index - 1][i, j] = data;
       //            }
       //    }
       //    //this.dgData.Items.Refresh();
       //}



       public void Confirm()
       {

           //以下改变三维数组的方式不会同步更新其最大最小值
           //for (int k = 0; k < this._lstData.Count; k++)
           //{
           //    for (int i = 0; i < this._dataset.Nrows; i++)
           //    {
           //        for (int j = 0; j < this._dataset.Ncols; j++)
           //        {
           //            this._dataset.Datas[i, j, k] = this._lstData[k][i, j];                       
           //        }                  
           //    }               
           //}

           if (this._dataset != null && this._lstData != null && this._lstData.Count > 0 && this._lstData[0] != null)
           {


               double[, ,] data = new double[this._lstData[0].GetLength(0), this._lstData[0].GetLength(1), this._lstData.Count];


               //更新 double[, ,] data

               for (int row = 0; row < data.GetLength(0); row++)
               {
                   for (int col = 0; col < data.GetLength(1); col++)
                   {
                       for (int layer = 0; layer < data.GetLength(2); layer++)
                       {
                           data[row, col, layer] = this._lstData[layer][row, col];
                       }
                   }
               }


               this._dataset.Update3DArryDatas(data);
           }

 
       }


    }
}
